﻿Imports System.Data.OleDb
Imports CrystalDecisions.Shared

Public Class VisualizadorRelatorio
    Inherits ComponentFactory.Krypton.Toolkit.KryptonForm

    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Sub New(ByVal doc As String, ByVal ds As DataSet)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        documento.Load(doc)
        documento.SetDataSource(ds)

        Me.viewer.RefreshReport()
        viewer.ReportSource = documento

    End Sub

    Private Sub VisualizadorRelatorio_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Se for um usuário comum, desativa o botão de impressão
        If (Login.usuario.Nivel = 2) Then
            viewer.ShowPrintButton = False
        End If

        viewer.Show()
    End Sub

    Public Sub gerarRelatorio(ByVal sql As String, ByVal nomeTabela As String, ByVal doc As String)
        Dim con As OleDbConnection = FabricaDeConexoes.CriarConexao()
        con.Open()
        Dim ds As DataSet = New DataSet()
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con)
        da.Fill(ds, nomeTabela)
        Dim relatorio As New VisualizadorRelatorio(doc, ds)
        relatorio.ShowDialog()
    End Sub

    Public Sub gerarRelatorio(ByVal sql As String, ByVal nomeTabela As String, ByVal doc As String, ByVal params As Hashtable)
        Dim con As OleDbConnection = FabricaDeConexoes.CriarConexao()
        con.Open()
        Dim ds As DataSet = New DataSet()
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(sql, con)
        da.Fill(ds, nomeTabela)
        Dim relatorio As New VisualizadorRelatorio(doc, ds)

        Dim docS = DirectCast(viewer.ReportSource, ParameterFields)

        For i = 0 To params.Count - 1
            docS(params.Keys(i).ToString).CurrentValues.Add(params.Item(params.Keys(i).ToString))
        Next

        relatorio.ShowDialog()
    End Sub

End Class